home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / genial / func / display.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-14  |  7.8 KB  |  335 lines

  1.  
  2. /*
  3.  * display.c -- routines for maintaining the display
  4.  */
  5.  
  6. #include "ui.h"
  7. #include "display.h"
  8. #include "sm.h"
  9. #include <math.h>
  10.  
  11. struct img_data *orig_img = NULL;
  12. XImage   *orig_ximg = NULL, *disp_ximg = NULL;
  13. int       shrink_fac = 0;    /* shrink factor.  Shrinks by 2^sfac. */
  14.  
  15. static int cmin, cmax;        /* colormap min and max */
  16. static float gam = 1.0;
  17.  
  18. /* routine to initialize the display window */
  19. init_display(ras)
  20.     struct img_data *ras;
  21. {
  22.     int       cshape = XC_crosshair;
  23.     Cursor    cursor;
  24.     XColor    stout, blk;
  25.  
  26.     /* initialize the colormap */
  27.     cmin = ras->minv;
  28.     cmax = ras->maxv;
  29.     xv_set(disp_ctrl->cmap_min,
  30.        PANEL_VALUE, cmin,
  31.        PANEL_MIN_VALUE, cmin,
  32.        PANEL_MAX_VALUE, cmax,
  33.        NULL);
  34.     xv_set(disp_ctrl->cmap_max,
  35.        PANEL_VALUE, cmax,
  36.        PANEL_MIN_VALUE, cmin,
  37.        PANEL_MAX_VALUE, cmax,
  38.        NULL);
  39.  
  40.     cmap_init();
  41.  
  42.     /* initialize the cursor color */
  43.     stout.red = stout.blue = 0;
  44.     stout.green = (255 << 8);
  45.     stout.pixel = standout;
  46.     stout.flags = blk.flags = DoRed | DoGreen | DoBlue;
  47.     blk.red = blk.green = blk.blue = 0;
  48.     blk.flags = DoRed | DoGreen | DoBlue;
  49.     blk.pixel = pallet[BLACK].pixel;
  50.  
  51.     cursor = XCreateFontCursor(display, cshape);
  52.     XDefineCursor(display, img_win->d_xid, cursor);
  53.     XRecolorCursor(display, cursor, &stout, &blk);
  54.  
  55. }
  56.  
  57. XImage   *
  58. mk_x_img(image, xim, use_shrink)
  59.     struct img_data *image;
  60.     XImage   *xim;
  61.     int       use_shrink;    /* boolean -- use shrink factor? */
  62. {
  63.     char     *xim_data = NULL, *dptr = NULL, *mapbuf = NULL;
  64.     register int x, y, shrink, i;
  65.     int       width, height;
  66.  
  67.     if (xim != NULL) {
  68.     free(xim->data);
  69.     XDestroyImage(xim);
  70.     }
  71.     if (use_shrink && shrink_fac > 0) {
  72.     mapbuf = calloc(image->width * image->height, sizeof(char));
  73.     map_image_to_colors((byte *) mapbuf, (byte *) image->lut,
  74.                 image->width * image->height);
  75.  
  76.     /* shink by powers of two */
  77.     shrink = shrink_fac << 1;
  78.     fprintf(stderr, "Shrinking image by %d ... \n", shrink);
  79.  
  80.     width = image->width / shrink;
  81.     height = image->height / shrink;
  82.  
  83.     xim_data = calloc(width * height, sizeof(char));
  84.     dptr = xim_data;
  85.  
  86.     i = 0;
  87.     for (y = 0; y < image->height; y += shrink) {
  88.         for (x = 0; x < image->width; x += shrink) {
  89.         /* just sub-sample for now, really should to averaging! */
  90.         dptr[i] = mapbuf[y * image->width + x];
  91.         i++;
  92.         }
  93.     }
  94.     free(mapbuf);
  95.     } else {
  96.     width = image->width;
  97.     height = image->height;
  98.     xim_data = calloc(width * height, sizeof(char));
  99.     map_image_to_colors((byte *) xim_data, (byte *) image->lut,
  100.                 width * height);
  101.     }
  102.  
  103.     switch (depth) {
  104.     case 8:
  105.     xim = XCreateImage(display, winv, depth,
  106.                ZPixmap, 0, xim_data, width, height, 8, 0);
  107.     break;
  108.     case 24:
  109.     case 32:
  110.     xim = XCreateImage(display, winv, depth,
  111.                ZPixmap, 0, xim_data, width, height, 32, 0);
  112.     break;
  113.     }
  114.     if (!xim) {
  115.     fprintf(stderr, "Error: couldn't create ximage!");
  116.     return (NULL);
  117.     }
  118.     return (xim);
  119. }
  120.  
  121. /*************************************************************/
  122. void
  123. disp_img()
  124. {
  125.  
  126. #ifdef DEBUG
  127.     printf("displaying image\n");
  128. #endif
  129.  
  130.     xv_set(img_win->d_win,
  131.        XV_SHOW, TRUE, FRAME_CLOSED, FALSE, NULL);
  132.  
  133.     xv_set(img_win->d_win,
  134.        XV_WIDTH, MIN((disp_ximg->width + SCROLL_BAR_SIZE), 512),
  135.        XV_HEIGHT, MIN((disp_ximg->height + SCROLL_BAR_SIZE), 512),
  136.        NULL);
  137.  
  138.     xv_set(img_win->d_canv,
  139.        CANVAS_AUTO_EXPAND, FALSE,
  140.        CANVAS_AUTO_SHRINK, FALSE,
  141.        OPENWIN_AUTO_CLEAR, TRUE,
  142.        CANVAS_FIXED_IMAGE, FALSE,
  143.        NULL);
  144.     xv_set(img_win->d_canv,
  145.        CANVAS_WIDTH, disp_ximg->width,
  146.        CANVAS_HEIGHT, disp_ximg->height,
  147.        NULL);
  148.  
  149.     imgwin_repaint_proc(img_win->d_canv, img_win->d_paintwin, display,
  150.             img_win->d_xid, NULL);
  151. }
  152.  
  153. /*
  154.  * Repaint callback function for `imgcanv'.
  155.  */
  156. imgwin_repaint_proc(canvas, paint_window, display, w, rects)
  157.     Canvas    canvas;
  158.     Xv_window paint_window;
  159.     Display  *display;
  160.     Window    w;
  161.     Xv_xrectlist *rects;
  162. {
  163. #ifdef DEBUG
  164.     printf(" in imgwin_repaint_proc \n");
  165. #endif
  166.  
  167.     if (disp_ximg == NULL)
  168.     return;
  169.  
  170.     XPutImage(display, w, gc, disp_ximg, 0, 0, 0, 0,
  171.           disp_ximg->width, disp_ximg->height);
  172.     draw_log();
  173. }
  174.  
  175. /***************************************************************/
  176. imgwin_resize_proc(win, event, arg, type)
  177.     Xv_window win;
  178.     Event    *event;
  179.     Notify_arg arg;
  180.     Notify_event_type type;
  181. {
  182. #ifdef DEBUG
  183.     fprintf(stderr, "genial: imgwin_resize_proc: event %d\n", event_id(event));
  184. #endif
  185.  
  186.     if (event_id(event) == WIN_RESIZE) {
  187.     if ((int) xv_get(img_win->d_win, XV_WIDTH) > disp_ximg->width +
  188.         SCROLL_BAR_SIZE)
  189.         xv_set(img_win->d_win,
  190.            XV_WIDTH, disp_ximg->width + SCROLL_BAR_SIZE, NULL);
  191.  
  192.     if ((int) xv_get(img_win->d_win, XV_HEIGHT) > disp_ximg->height +
  193.         SCROLL_BAR_SIZE)
  194.         xv_set(img_win->d_win,
  195.            XV_HEIGHT, disp_ximg->height + SCROLL_BAR_SIZE, NULL);
  196.     }
  197.     return;
  198. }
  199.  
  200. /*****************************************************/
  201.  
  202. /* the following function sets / changes the gamma value */
  203. set_gam(v)
  204.     float     v;
  205. {
  206.     gam = v;
  207.     if (getstate() == IMG_UNLOADED)
  208.     return;
  209.  
  210.     apply_gamma(gam, 0, 0, NCOLORS, 0, 0);
  211.  
  212.     map_image_to_colors((byte *) disp_ximg->data, (byte *) orig_img->lut,
  213.             disp_ximg->width * disp_ximg->height);
  214.  
  215.     imgwin_repaint_proc(img_win->d_canv, img_win->d_paintwin, display,
  216.             img_win->d_xid, NULL);
  217. }
  218.  
  219. /*****************************************************/
  220. set_cmin(v)
  221.     int       v;
  222. {
  223.     cmin = v;
  224.     if (getstate() == IMG_UNLOADED)
  225.     return;
  226.  
  227.     /*
  228.      * this just sets all pixels outside the range to black. Might be better
  229.      * to actually rescale the image using the new values: ie: and min and
  230.      * max arguements to 'make_lut' (cmap.c), then call build_colomap again
  231.      * (SLOW!)
  232.      */
  233.     remap_image(disp_ximg->data, orig_img->data, orig_img->lut,
  234.         orig_img->width * orig_img->height,
  235.         cmin, cmax);
  236.  
  237.     imgwin_repaint_proc(img_win->d_canv, img_win->d_paintwin, display,
  238.             img_win->d_xid, NULL);
  239.  
  240. }
  241.  
  242. /*****************************************************/
  243. set_cmax(v)
  244.     int       v;
  245. {
  246.     cmax = v;
  247.     if (getstate() == IMG_UNLOADED)
  248.     return;
  249.  
  250.     remap_image(disp_ximg->data, orig_img->data, orig_img->lut,
  251.         orig_img->width * orig_img->height,
  252.         cmin, cmax);
  253.  
  254.     imgwin_repaint_proc(img_win->d_canv, img_win->d_paintwin, display,
  255.             img_win->d_xid, NULL);
  256. }
  257.  
  258. /************************************************/
  259. /* map image to color indexes */
  260. remap_image(mapped_image, data, lut, size, min, max)
  261.     byte     *mapped_image, *data, *lut;
  262.     int       size;
  263. {
  264.     register int i;
  265.  
  266.     for (i = 0; i < size; i++) {
  267.     if (winv->class == TrueColor) {    /* 24-bit systems */
  268. #ifdef LATER
  269.         /* need to do something here!! */
  270. #endif
  271.  
  272.     } else {
  273.         if (data[i] < min || data[i] > max)
  274.         mapped_image[i] = pallet[BLACK].pixel;
  275.         else
  276.         mapped_image[i] = (byte) colors[lut[i]];
  277.     }
  278.     }
  279. }
  280.  
  281. /****************************************************************
  282.  * the following routines are for dealing with display images which differ
  283.  *  from the original image
  284.  *
  285.  */
  286.  
  287. /* set_shrink_fac() sets the shrink factor to n.  This shrinks the image by
  288.  *  2^n.
  289.  *
  290.  */
  291. set_shrink_fac(n)
  292.     int       n;
  293. {
  294.     shrink_fac = n;
  295.     if (getstate() == IMG_UNLOADED)
  296.     return;
  297.     disp_ximg = mk_x_img(orig_img, disp_ximg, 1);
  298.     disp_img();
  299. }
  300.  
  301. /* display to image coordinates */
  302. /* dtoi_pt() and itod_pt() are routines that convert XPoints from display to
  303. image coordinates and vice versa. dtoi() and itod() do this same conversion
  304. with simple ints, and are useful for converting things like the width of a
  305. cross */
  306.  
  307. dtoi_pt(dsp, img)
  308.     XPoint   *dsp, *img;
  309. {
  310.     img->x = (dsp->x << shrink_fac);
  311.     img->y = (dsp->y << shrink_fac);
  312. }
  313.  
  314. /* and the reverse */
  315. itod_pt(img, dsp)
  316.     XPoint   *img, *dsp;
  317. {
  318.     dsp->x = (img->x >> shrink_fac);
  319.     dsp->y = (img->y >> shrink_fac);
  320. }
  321.  
  322. int
  323. dtoi(v)
  324.     int       v;
  325. {
  326.     return (v << shrink_fac);
  327. }
  328.  
  329. int
  330. itod(v)
  331.     int       v;
  332. {
  333.     return (v >> shrink_fac);
  334. }
  335.